package com.tf.xx.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;

import com.tf.xx.entity.EduCourse;
import com.tf.xx.entity.EduCourseDescription;
import com.tf.xx.entity.vo.*;
import com.tf.xx.exception.MyException;
import com.tf.xx.mapper.EduCourseDescriptionMapper;
import com.tf.xx.mapper.EduCourseMapper;
import com.tf.xx.service.EduChapterService;
import com.tf.xx.service.EduCourseDescriptionService;
import com.tf.xx.service.EduCourseService;
import com.tf.xx.service.EduVideoService;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService{
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;


    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int result = baseMapper.insert(eduCourse);

        String cid = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(cid);
        return cid;
    }

    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //定义一个空的课程对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //根据ID查询对象
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //属性copy
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        //查询简介并赋值
        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());

        return courseInfoForm;

    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        baseMapper.updateById(eduCourse);
        //2.修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }



    @Override
    public boolean removeCourse(String id) {
        //根据课程id删除小节
        eduVideoService.removeVideoByCourseId(id);
        //根据课程id删除章节部分
        eduChapterService.removeChapterByCourseId(id);
        //根据课程id删除课程描述
        eduCourseDescriptionService.removeById(id);
        int result = baseMapper.deleteById(id);
        if (result == 1){
            return true;
        }else {
            throw new MyException(200001,"删除失败");
        }
    }
    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery){
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!title.isEmpty()){
            queryWrapper.like("title", title);

        }
        if (!status.isEmpty()){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("gmt_modified");
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseid) {
        return baseMapper.getBaseCourseInfo(courseid);
    }

    @Override
    @Cacheable( key = "'selectHotCourse'",value = "course")
    public List<EduCourse> selectHotCourse() {
        //查询前8条热门课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);

    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        //拼接条件查询
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if(StringUtils.isNotEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if(StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if(StringUtils.isNotEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        eduCourseMapper.selectPage(pageCourse, wrapper);
        Map<String, Object> map = new HashMap<>();
        long total = pageCourse.getTotal(); // 总记录数
        List<EduCourse> courseList = pageCourse.getRecords(); // 数据集合
        long size = pageCourse.getSize(); // 每页记录数
        long current = pageCourse.getCurrent(); // 当前页
        long pages = pageCourse.getPages(); // 总页数
        boolean hasPrevious = pageCourse.hasPrevious(); // 是否有上一页
        boolean hasNext = pageCourse.hasNext(); // 是否有下一页
        map.put("total", total);
        map.put("list", courseList);
        map.put("size", size);
        map.put("current", current);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);
        return map;

    }

}
